<template>
  <div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange"
    >
      <span slot="action" slot-scope="text, record">
        <a-icon type="vertical-align-bottom" @click="handleDownloadFile(record.url)" ></a-icon>
        <a-icon v-if="isable" style="margin-left: 10px" type="delete" @click="handleRemoveFile(record)" ></a-icon>
      </span>
      <span slot="actionName">
        <a-button size="small" @click="openCreatModel()" type="primary" icon="plus" :disabled="!isable">新增</a-button>
      </span>
    </a-table>

    <FileModel ref="fileModel" @openModelView="handleFileAdd"></FileModel>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { columns2 } from '../indexColumns'
import FileModel from '../FileModel.vue'
import { getAction } from '../../../../../../api/manage'
export default {
  mixins: [JeecgListMixin],
  props: ['planId', 'isable'],
  components: { FileModel },
  data() {
    return {
      columns: columns2(this),
      dataSource: [],
      url: {
        // list: '/sjzyzhgl/sjAuditOrg/list'
        list: '/incon/inconReviewPlan/queryFileList',
        remove: '/sjzyzhgl/sjFileInfo/removeAffix',
      },
      disableMixinCreated: true,
    }
  },
  methods: {
    // getTableData(row) {
    //   debugger
    //   this.url.list = `${this.url.list}&planId=${val}&fileType=yearPlan`
    //   this.loadData()
    // },
    handleDownloadFile(fileUrl) {
      this.$message.success('正在下载，请稍等！')
      let url = `${window._CONFIG['domianURL']}/sys/common/download/${fileUrl}`
      window.open(url, '_blank')
    },
    openCreatModel() {
      // this.$refs.fileModel.planId = this.planId
      this.$refs.fileModel.fid = ''
      this.$refs.fileModel.visible = true
    },
    // TODO 上传
    handleFileAdd(id) {
      this.$refs.fileModel.handleFileAdd(this.planId, 'draft')
      this.$refs.fileModel.visible = false
      setTimeout(() => {
        this.loadData()
      }, 500)
    },
    handleRemoveFile(record) {
      getAction(this.url.remove, { id: record.id }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
          return true
        } else {
          this.$message.error(res.message)
          return false
        }
      })
    },
  },
  watch: {
    planId: {
      handler(val) {
        this.url.list = `${this.url.list}?planId=${val}&fileType=draft`
        this.loadData()
      },
      deep: true,
      immediate: true,
    },
  },
}
</script>

<style>
</style>